package se.webbzon.boltzmann.configuration;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collection;

import se.webbzon.boltzmann.debug.Debug;

public class Graphics {
	
	// The singleton instance of the graphic configuration
	private static Graphics singleton;
	
	// The resolution mode
	public enum Resolution {LOW, MEDIUM, HIGH};
	
	// A collection of all the listeners to the graphic configuration instance
	private final Collection<GraphicsListener> listeners;
	
	// Whatever shadows are allowed
	private boolean shadows;
	
	// The resolution for the heads-up display
	private Resolution resolution;
	
	/*============================================================
	Constructors
	============================================================*/
	
	private Graphics() {
		listeners = new ArrayList<GraphicsListener>();
		this.resolution = Resolution.LOW;
		this.shadows = true;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the singleton instance of the graphic configuration. **/
	public synchronized static Graphics instance() {
		if (singleton == null)
			singleton = new Graphics();
		return singleton;
	}
	
	/** Adds a listener to the graphic configuration instance. **/
	public boolean addListener(GraphicsListener listener) {
		int d;
		Debug.debugCollection(this, listeners);
		return listeners.add(listener);
	}
	
	/** Removes a listener from the graphic configuration instance. **/
	public boolean removeListener(GraphicsListener listener) {
		return listeners.remove(listener);
	}
	
	/** Clears all listeners to the graphics configuration instance. **/
	public void clearListeners() {
		listeners.clear();
	}
	
	/** Turns on and off shadow creation in the game. **/
	public void setShadows(boolean shadows) {
		if (this.shadows != shadows) {
			this.shadows = shadows;
			for (GraphicsListener listener : listeners.toArray(new GraphicsListener[0]))
				listener.onSetShadows(shadows);
		}
	}
	
	/** Returns true if shadows are allowed. **/
	public boolean hasShadows() {
		return shadows;
	}
	
	/** Sets the resolution for the heads-up display. **/
	public void setResolution(Resolution resolution) {
		if (this.resolution != resolution) {
			this.resolution = resolution;
			for (GraphicsListener listener : listeners.toArray(new GraphicsListener[0]))
				listener.onSetResolution(resolution);
		}
	}
	
	/** Returns the resolution for the heads-up display. **/
	public Resolution getResolution() {
		return resolution;
	}
	
	/** Copies the dimension of the heads-up display to the provided
	 * dimension instance. **/
	public void getDimension(Dimension d) {
		switch (Graphics.instance().getResolution()) {
		case LOW:
			d.setSize(640, 360);
			return;
		case MEDIUM:
			d.setSize(960, 540);
			return;
		case HIGH:
			d.setSize(1280, 720);
			return;
		}
	}

}
